<template>
  <div class="home">
    <main>
      <el-carousel height="400px" class="banner" :interval="5000" arrow="always">
        <el-carousel-item v-for="item in responsiveBanners" :key="item.id">
          <img
            :src="item.displayImage"
            :alt="item.title"
            class="banner-image"
            :style="{ height: windowWidth < 768 ? '300px' : '400px' }"
          >
          <div class="banner-content">
            <h2>{{ item.title }}</h2>
            <p>{{ item.desc }}</p>
          </div>
        </el-carousel-item>
      </el-carousel>

      <div class="container">
        <section class="categories-section">
          <h2 class="section-title">热门分类</h2>
          <el-row :gutter="20">
            <el-col
              :xs="24"
              :sm="windowWidth < 768 ? 24 : 12"
              :md="windowWidth < 992 ? 8 : 6"
              v-for="category in categories"
              :key="category.id"
            >
              <el-card shadow="hover" class="category-card">
                <div class="category-icon">
                  <i :class="category.icon"></i>
                </div>
                <h3>{{ category.name }}</h3>
                <p>{{ category.desc }}</p>
                <el-button
                  v-if="windowWidth >= 768"
                  type="text"
                  size="small"
                  @click="navigateToCategory(category.id)"
                >
                  查看详情
                </el-button>
              </el-card>
            </el-col>
          </el-row>
        </section>

        <section class="courses-section">
          <h2 class="section-title">精选课程</h2>
          <el-row :gutter="20">
            <el-col
              :xs="24"
              :sm="windowWidth < 768 ? 24 : 12"
              :md="windowWidth < 992 ? 12 : 8"
              v-for="course in courses"
              :key="course.id"
            >
              <el-card shadow="hover" class="course-card">
                <div class="course-image-wrapper">
                  <img
                    :src="course.image"
                    :alt="course.title"
                    class="course-image"
                    :style="{ height: windowWidth < 768 ? '150px' : '200px' }"
                  >
                  <div class="course-tag" v-if="course.tag">{{ course.tag }}</div>
                </div>
                <div class="course-info">
                  <h3>{{ course.title }}</h3>
                  <div class="price">¥{{ course.price }}
                    <span class="original-price" v-if="course.originalPrice">¥{{ course.originalPrice }}</span>
                  </div>
                  <div class="meta">
                    <span><i class="el-icon-user"></i> {{ course.students }}</span>
                    <span><i class="el-icon-star-off"></i> {{ course.rating }}</span>
                  </div>
                  <el-button
                    type="primary"
                    :size="windowWidth < 768 ? 'mini' : 'small'"
                    class="enroll-btn"
                  >
                    立即报名
                  </el-button>
                </div>
              </el-card>
            </el-col>
          </el-row>
        </section>
      </div>
    </main>

    <footer class="footer">
      <div class="container">
        <div class="footer-content">
          <div class="footer-section">
            <h3>关于我们</h3>
            <p>HIIT在线学习平台致力于为学习者提供优质的在线课程资源。</p>
          </div>
          <div class="footer-section">
            <h3>联系方式</h3>
            <p>邮箱：contact@hiit.com</p>
            <p>电话：400-123-4567</p>
          </div>
          <div class="footer-section">
            <h3>关注我们</h3>
            <div class="social-links">
              <i class="el-icon-s-platform"></i>
              <i class="el-icon-s-custom"></i>
              <i class="el-icon-s-promotion"></i>
            </div>
          </div>
        </div>
        <div class="copyright">
          <p>© 2023 HIIT在线学习平台 版权所有</p>
        </div>
      </div>
    </footer>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
import { debounce } from 'lodash-es';

const router = useRouter();

// 响应式状态
const activeIndex = ref('1');
const windowWidth = ref(window.innerWidth);

// 轮播图数据
const banners = reactive([
  {
    id: 1,
    title: '编程课程',
    desc: '掌握前沿编程技术，提升开发能力',
    image: 'https://picsum.photos/1200/400?random=1',
    mobileImage: 'https://picsum.photos/600/300?random=1'
  },
  {
    id: 2,
    title: '设计课程',
    desc: '学习专业设计技巧，创造视觉艺术',
    image: 'https://picsum.photos/1200/400?random=2',
    mobileImage: 'https://picsum.photos/600/300?random=2'
  },
  {
    id: 3,
    title: '营销课程',
    desc: '了解市场趋势，掌握营销策略',
    image: 'https://picsum.photos/1200/400?random=3',
    mobileImage: 'https://picsum.photos/600/300?random=3'
  }
]);

// 分类数据
const allCategories = reactive([
  { id: 1, name: '编程开发', desc: '学习最新编程技术', icon: 'el-icon-cpu' },
  { id: 2, name: '设计创意', desc: '提升设计能力', icon: 'el-icon-brush' },
  { id: 3, name: '营销推广', desc: '掌握营销技巧', icon: 'el-icon-data-line' },
  { id: 4, name: '职场技能', desc: '提升职场竞争力', icon: 'el-icon-office-building' },
  { id: 5, name: '语言学习', desc: '掌握多国语言', icon: 'el-icon-chat-line-round' },
  { id: 6, name: '考试认证', desc: '获取专业认证', icon: 'el-icon-document' }
]);

// 课程数据
const allCourses = reactive([
  {
    id: 1,
    title: 'Vue 3 实战开发',
    image: 'https://picsum.photos/400/300?random=4',
    price: 299,
    originalPrice: 599,
    tag: '限时优惠',
    students: '1254人学习',
    rating: '4.9'
  },
  {
    id: 2,
    title: 'React 高级课程',
    image: 'https://picsum.photos/400/300?random=5',
    price: 399,
    students: '986人学习',
    rating: '4.8'
  },
  {
    id: 3,
    title: 'UI设计基础',
    image: 'https://picsum.photos/400/300?random=6',
    price: 199,
    students: '2156人学习',
    rating: '4.7'
  },
  {
    id: 4,
    title: 'Python数据分析',
    image: 'https://picsum.photos/400/300?random=7',
    price: 349,
    students: '876人学习',
    rating: '4.8'
  },
  {
    id: 5,
    title: 'Flutter移动开发',
    image: 'https://picsum.photos/400/300?random=8',
    price: 399,
    students: '654人学习',
    rating: '4.7'
  }
]);

// 计算属性
const categories = computed(() => {
  if (windowWidth.value < 768) {
    return allCategories.slice(0, 2);
  } else if (windowWidth.value < 992) {
    return allCategories.slice(0, 3);
  }
  return allCategories.slice(0, 4);
});

const courses = computed(() => {
  if (windowWidth.value < 768) {
    return allCourses.slice(0, 2);
  }
  return allCourses.slice(0, 3);
});

const responsiveBanners = computed(() => {
  return banners.map(banner => ({
    ...banner,
    displayImage: windowWidth.value < 768 ? banner.mobileImage : banner.image
  }));
});

// 方法
const handleSelect = (key) => {
  if (key === '2') {
    goToCourses();
  }
};

const goToCourses = () => {
  router.push({ name: 'Courses' });
};

const navigateToCategory = (categoryId) => {
  router.push({ name: 'CategoryDetail', params: { id: categoryId } });
};

const handleResize = debounce(() => {
  windowWidth.value = window.innerWidth;
  console.log('Window resized to:', windowWidth.value);
}, 200);

// 生命周期钩子
onMounted(() => {
  handleResize();
  window.addEventListener('resize', handleResize);
});

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize);
});
</script>

<style scoped>
.home {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
}

.container {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
  transition: all 0.3s ease;
}

.header {
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.logo {
  font-size: 1.5rem;
  font-weight: bold;
  color: #409EFF;
}

.user-actions {
  display: flex;
  gap: 10px;
}

main {
  flex: 1;
}

.banner {
  width: 100%;
  position: relative;
  margin-bottom: 40px;
}

.banner-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.banner-content {
  position: absolute;
  bottom: 20%;
  left: 10%;
  color: white;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
  z-index: 2;
}

.banner-content h2 {
  font-size: 2.5rem;
  margin-bottom: 10px;
}

.banner-content p {
  font-size: 1.2rem;
}

.section-title {
  font-size: 2rem;
  margin-bottom: 30px;
  text-align: center;
  color: #303133;
  position: relative;
}

.section-title::after {
  content: '';
  display: block;
  width: 80px;
  height: 3px;
  background: #409EFF;
  margin: 10px auto;
}

.categories-section, .courses-section {
  margin-bottom: 60px;
  padding: 40px 0;
  background-color: #f9f9f9;
}

.courses-section {
  background-color: #fff;
}

.category-card {
  height: 100%;
  text-align: center;
  padding: 20px;
  transition: transform 0.3s;
}

.category-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.category-icon {
  font-size: 48px;
  color: #409EFF;
  margin-bottom: 15px;
}

.category-card h3 {
  margin-bottom: 10px;
  color: #303133;
}

.category-card p {
  color: #606266;
}

.course-card {
  height: 100%;
  margin-bottom: 20px;
  overflow: hidden;
  transition: all 0.3s;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.course-image-wrapper {
  position: relative;
  overflow: hidden;
}

.course-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
  transition: transform 0.3s;
}

.course-card:hover .course-image {
  transform: scale(1.05);
}

.course-tag {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #F56C6C;
  color: white;
  padding: 5px 10px;
  border-radius: 4px;
  font-size: 12px;
}

.course-info {
  padding: 15px;
}

.course-info h3 {
  margin-bottom: 10px;
  color: #303133;
  font-size: 16px;
  height: 44px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.price {
  color: #F56C6C;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.original-price {
  color: #909399;
  font-size: 14px;
  text-decoration: line-through;
  margin-left: 5px;
}

.meta {
  display: flex;
  justify-content: space-between;
  color: #909399;
  font-size: 12px;
  margin-bottom: 15px;
}

.enroll-btn {
  width: 100%;
}

.footer {
  background-color: #303133;
  color: #E4E7ED;
  padding: 40px 0;
  margin-top: auto;
}

.footer-content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin-bottom: 20px;
}

.footer-section {
  min-width: 30%;
  margin-bottom: 20px;
}

.footer-section h3 {
  margin-bottom: 15px;
  color: #fff;
  font-size: 18px;
}

.social-links {
  display: flex;
  gap: 15px;
  font-size: 24px;
}

.social-links i {
  cursor: pointer;
  transition: color 0.3s;
}

.social-links i:hover {
  color: #409EFF;
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .container {
    padding: 0 30px;
  }
}

@media (max-width: 992px) {
  .el-menu-demo {
    display: none;
  }
  
  .banner-content h2 {
    font-size: clamp(1.5rem, 4vw, 2rem);
  }
  
  .banner-content p {
    font-size: clamp(0.9rem, 2.5vw, 1rem);
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 15px;
  }
  
  .logo {
    font-size: clamp(1rem, 4vw, 1.2rem);
  }
  
  .user-actions .el-button {
    padding: 8px 12px;
    font-size: 12px;
  }
  
  .banner {
    height: clamp(250px, 50vw, 300px);
  }
  
  .banner-content {
    bottom: 10%;
    left: 5%;
  }
  
  .section-title {
    font-size: 1.5rem;
  }
  
  .footer-section {
    min-width: 100%;
    text-align: center;
  }
}

@media (max-width: 576px) {
  .banner {
    height: 220px;
  }
  
  .banner-content h2 {
    font-size: 1.2rem;
    margin-bottom: 5px;
  }
  
  .banner-content p {
    font-size: 0.9rem;
  }
  
  .category-icon {
    font-size: 36px;
  }
  
  .course-image {
    height: 150px;
  }
  
  .course-info h3 {
    font-size: 14px;
    height: 38px;
  }
  
  .price {
    font-size: 16px;
  }
  
  .meta {
    font-size: 11px;
  }
  
  .enroll-btn {
    font-size: 12px;
    padding: 8px 12px;
  }
}
</style>